/*
 * InputFile.java
 *
 * 
 */
package gui;

import java.io.File;
//import java.io.FileFilter;
import javax.swing.JFileChooser;//
import main.ToEngineFacade;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author  Elad
 */
public class InputFileChooser extends javax.swing.JFileChooser {

    private ToEngineFacade toEngineFacade;

    /** Creates new form InputFile */
    public InputFileChooser(ToEngineFacade toEngFacade) {
        initComponents();
        toEngineFacade = toEngFacade;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {


        setCurrentDirectory(new File(System.getProperty("user.dir")));
        setFileFilter(ff);
    }// </editor-fold>//GEN-END:initComponents
    
    public void performAction(){
    	File f = getSelectedFile();
    	 toEngineFacade.setFileNameFromUser(f);
    }

    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new InputFile().setVisible(true);
    }
    });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    //   FileFilter filter = new
    //   fileNameChooser.addChoosableFileFilter(filter);
/*FileFilter f = new FileFilter() {
    
    public boolean accept(File pathname) {
    // Since this is used during enumeration of existing file system,
    // this should not be necessary, but good practice to test for null.
    
    
    return false;
    }
    
    @Override
    public String getDescription() {
    throw new UnsupportedOperationException("Not supported yet.");
    }
    };*/

    public static void addDBFileFIlter() {
        /*fileNameChooser.addChoosableFileFilter(new FileFilter() {

        @Override
        public boolean accept(File f) {
        throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getDescription() {
        throw new UnsupportedOperationException("Not supported yet.");
        }
        });*/
    }
    FileFilter ff = new FileFilter() {

        @Override
        public boolean accept(File f) {

            // Since this is used during enumeration of existing file system,
            // this should not be necessary, but good practice to test for null.
            if (f != null) {
                if (f.isDirectory()) { // Allow directories to be seen.
                    return true;
                }
                String filename = f.getName();
//                String name = filename.substring(0, filename.lastIndexOf("."));
                String suffix = filename.substring(filename.lastIndexOf(".") + 1);
                if (suffix.equals("csv")) {
                    return true;
                }
                if (suffix.equals("db")) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public String getDescription() {
            return "*.db";
        }
    };
}

